package org.nightrunner.d3.task;

import org.apache.log4j.Logger;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.search.FullTextSession;
import org.hibernate.search.Search;
import org.nightrunner.d3.log.SysLog;

public class RebuildHibernateSearchIndexTask {

	private static final Logger LOGGER = SysLog.SYSTEM;

	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public void work() {
		LOGGER.info("开始重新建立索引...");
		long start = System.currentTimeMillis();
		Session session = sessionFactory.openSession();
		FullTextSession fullTextSession = Search.getFullTextSession(session);
		try {
			fullTextSession.createIndexer().startAndWait();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		LOGGER.info("重建索引完成，共用时:[" + (System.currentTimeMillis() - start)
				+ "]毫秒");
	}

}
